﻿/*
 *   NameSolver.cs
 *   Copyright (c) 2011 - BitFox Software and Ricardo Federico Villafañes
 *   Contact: federvillaf@hotmail.com
 *   
 *   Licensing: This product is covered by Microsoft Reciprocal License (MS-RL)
 *              see https://bitfoxeval.codeplex.com/license
*/

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.CodeAnalysis;

using BitFox.Evaluator.Functions;

namespace BitFox.Evaluator
{
    /// <summary>
    /// The kind of operation that a solver must to handle
    /// </summary>
    public enum NameOperation 
    { 
        /// <summary>
        /// An operation to <b>GET</b> the value of a variable is requested.
        /// </summary>
        Get, 

        /// <summary>
        /// An operation to <b>SET</b> the value of a variable is requested.
        /// </summary>
        Set 
    }

    /// <summary>
    /// Class that encapsulates the request of variable, for externals solvers
    /// </summary>
    public class NameRequest
    {
        /// <summary>
        /// Field that stores the result of the request, or the new value when a variable is set.
        /// </summary>
        object v = null;

        /// <summary>
        /// The variable/array name
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Value used on result to known the result for operation. It's initialized to false.
        /// </summary>
        public bool Resolved { get; set; }

        /// <summary>
        /// The collection of indices that point to a given item in an array of objects. If the item is scalar,
        /// this property is null.
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1819")]
        public int[] Indexes { get; private set; }

        /// <summary>
        /// The operation that is requested
        /// </summary>
        public NameOperation Operation { get; internal set; }

        /// <summary>
        /// If the operation is 'Get', this property returns the value stored in requested var. If the operation
        /// is 'Set', this poperty is used to store the new value for the variable.
        /// </summary>
        public object Value
        {
            get { return v; }
            set
            {
                Resolved = (value != null);
                v = value;
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">The variable's name</param>
        /// <param name="indexes">A collection of indices to access array's items. If the requested name is not an
        /// array, this argument will be null.</param>
        public NameRequest(string name, int[] indexes)
        {
            Name = name;
            Indexes = indexes;
            Value = null;
            Operation = NameOperation.Get;
        }
    }
}
